/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import "../../../../shared/qml/UIConstants.js" as UIConstants
import NewsBlur 1.0
import WebFeeds 1.0

Item
{
    id:root
    property int negativeCount: 0
    property int neutralCount: 0
    property int positiveCount: 0
    property bool largeSized: false
    property bool vertical: false
    property int filterType: NewsBlurEnum.CfNoFilter
    property bool hideNullSegments: true
    width: row.width
    height: row.height

    Row {
        id: row
//        anchors.fill: parent
        visible: !root.vertical
        Item {
            id: leftBubble
            //width: visible ? internal.getBubbleWidth(root.negativeCount, leftText.paintedWidth) : 0
            width: visible ? leftText.paintedWidth + 13 : 0
            visible: root.hideNullSegments ? (root.negativeCount > 0) : true
            height: 24
            BorderImage {
                source: {
                    if(root.hideNullSegments) {
                        if(middleBubble.visible || rightBubble.visible)
                            return "qrc:/plugins/newsblur/images/count_bubble/red_horizontal_left.png";
                        else
                            return "qrc:/plugins/newsblur/images/count_bubble/red.png";
                    } else {
                        if(root.filterType == NewsBlurEnum.CfNoFilter)
                            return "qrc:/plugins/newsblur/images/count_bubble/red_horizontal_left.png"
                        else
                            return "qrc:/plugins/newsblur/images/count_bubble/gray_horizontal_left.png"
                    }

                }
                anchors.fill: parent
                border { left: 10; top: 10; right: 10; bottom: 10 }
            }
            Text {
                id: leftText
                height: parent.height
                y:1
                color: "white"
                font.family: UIConstants.FONT_FAMILY_BOLD
                anchors.horizontalCenter: parent.horizontalCenter
                verticalAlignment: Text.AlignVCenter
                font.pixelSize: 18
                text: root.negativeCount
            }
        }
        Item {
            id: middleBubble
//            width: visible ? internal.getBubbleWidth(root.neutralCount, middleText.paintedWidth) : 0
            width: visible ? middleText.paintedWidth + 13 : 0
            visible: root.hideNullSegments ? (root.neutralCount > 0) : true
            height: 24
            BorderImage {
                source: {
                    if(root.hideNullSegments) {
                        if(leftBubble.visible && rightBubble.visible)
                            return "qrc:/plugins/newsblur/images/count_bubble/orange_horizontal_middle.png";
                        else if(leftBubble.visible)
                            return "qrc:/plugins/newsblur/images/count_bubble/orange_horizontal_right.png";
                        else if(rightBubble.visible)
                            return "qrc:/plugins/newsblur/images/count_bubble/orange_horizontal_left.png";
                        else
                            return "qrc:/plugins/newsblur/images/count_bubble/orange.png";
                    } else {
                        if(root.filterType != NewsBlurEnum.CfOnlyPositive)
                            return "qrc:/plugins/newsblur/images/count_bubble/orange_horizontal_middle.png"
                        else
                            return "qrc:/plugins/newsblur/images/count_bubble/gray_horizontal_middle.png"
                    }
                }
                anchors.fill: parent
                border { left: 10; top: 10; right: 10; bottom: 10 }
            }
            Text {
                id: middleText
                height: parent.height
                y:1
                color: "white"
                font.family: UIConstants.FONT_FAMILY_BOLD
                anchors.horizontalCenter: parent.horizontalCenter
                verticalAlignment: Text.AlignVCenter
                font.pixelSize: 18
                text: root.neutralCount
            }
        }
        Item {
            id: rightBubble
//            width: visible ? internal.getBubbleWidth(root.positiveCount, rightText.paintedWidth) : 0
            width: visible ? rightText.paintedWidth + 13 : 0
            visible: root.hideNullSegments ? (root.positiveCount > 0) : true
            height: 24
            BorderImage {
                source: {
                    if(root.hideNullSegments) {
                    if(middleBubble.visible || leftBubble.visible)
                        return "qrc:/plugins/newsblur/images/count_bubble/green_horizontal_right.png";
                    else
                        return "qrc:/plugins/newsblur/images/count_bubble/green.png";
                    } else {
                        return "qrc:/plugins/newsblur/images/count_bubble/green_horizontal_right.png";
                    }
                }
                anchors.fill: parent
                border { left: 10; top: 10; right: 10; bottom: 10 }
            }
            Text {
                id: rightText
                height: parent.height
                y:1
                color: "white"
                font.family: UIConstants.FONT_FAMILY_BOLD
                anchors.horizontalCenter: parent.horizontalCenter
                verticalAlignment: Text.AlignVCenter
                font.pixelSize: 18
                text: root.positiveCount
            }
        }
    }

    QtObject {
        id: internal
        function getBubbleWidth(value, paintedWidth) {
            if (value < 10)
                return 24;
            else if (value < 100)
                return 30;
            else if (value < 1000)
                return 40;
            else
                return paintedWidth+13
        }
    }
}
